$(document).ready(function(){
	//global vars
	var form = $("#formId")
	var name = $("#userName");
	var email = $("#email");
	var password = $("#password");
	var password2 = $("#password2");
	var invite = $("#invite");
	var random = $("#random");
	var ajaxPetName = $("#ajaxPetName")
	var ajaxEmail = $("#ajaxEmail")
	var chk = $("#checkbuttion");
	//On blur
	name.blur(validateName);
	email.blur(validateEmail);
	password.blur(validatePassword);
	password2.blur(validatePassword2);
	invite.blur(validateInvite);
	random.blur(validateRandom);
	form.submit(function(){
		if(ajaxPetName.val()!=1){
			return false;
		}else if(ajaxEmail.val()!=1){
			return false;
		}else if(validatePassword() && validatePassword2()&& validateInvite() && validateRandom()&& chk.attr("checked")=='checked'){
		$("#buttionSubmit").attr("disabled",true);  
		$.ajax({
		   type: "POST",
		   dataType:'json',
		   cache:false,
		   url: "member_user_reg.htm?"+$(this).serialize(),
		   success:function(data){
		   		//$(this).attr("disabled",true);
		   		if(data.result){
		   			window.location.href='./ucenter_ucenter_stepOne.htm';
		   		}else{
		   			alert(data.msg);
		   		}
		   }
		});
		$("#buttionSubmit").attr("disabled",false);  
	     return false;
	    }else{
	    	return false;
	    } 
	}); 
	
	function validateName(){
		if(name.val()==''){
			name.next().next().html("用户昵称不能为空").show();
			return false;
		}else if(!isPetName(name.val())){
			name.next().next().html("用户昵称格式不正确").show();
			return false;
		}else{
			isExist(name,'用户昵称已存在',ajaxPetName);
		}
	}
	function validateEmail(){
		if(email.val()==''){
			email.next().next().html("邮箱不能为空").show();
			return false;
		}else if(!isEmail(email.val())){
			email.next().next().html("邮箱格式不正确").show();
			return false;
		}else{
			isExist(email,'邮箱已被占用',ajaxEmail);
		}
	}
	
	function validatePassword(){
		if(password.val()==''){
			password.next().next().html("密码不能为空").show();
			return false;
		}else if(!isPassword(password.val())){
			password.next().next().html("密码为6-16为数字或者字母组合").show();
			return false;
		}else{
			password.next().html('OK');
			password.next().next().hide();
			return true;
		}
	}
	
	function validatePassword2(){
		if(password.val()==''){
			password2.next().next().html("确认密码不能为空").show();
			return false;
		}else if(password2.val()!=password.val()){
			password2.next().next().html("两次密码输入不一至").show();
			return false;
		}else{
			password2.next().html('OK');
			password2.next().next().hide();
			return true;
		}
	}
	
	function validateInvite(){
		if(invite.val()==''){
			invite.next().next().html("邀请码不能为空").show();
			return false;
		}else{
			invite.next().html('OK');
			invite.next().next().hide();
			return true;
		}
	}
	function validateRandom(){
		if(random.val()==''||random.val().length!=4){
			random.next().next().html("请输入右侧提示字符").show();
			return false;
		}else{
			random.next().next().html('OK');
			random.next().next().hide();
			return true;
		}
	}
	});
	function isExist(obj,errMsg,hid){
	hid.val(2);
	$.ajax({
		   type: "POST",
		   dataType:'json',
		   url: "member_user_validateUser.htm",
		   data:{'validate':obj.val()},
		   success:function(data){
		   		if(data.result){
		   			obj.next().html('OK');
					obj.next().next().hide();
					hid.val(1);
		   		}else{
		   			hid.val(0);
		   			obj.next().html('');
					obj.next().next().html(errMsg).show();
		   		}
		   }
		});
	}	